const express = require('express');
const path = require('path');
const http = require('http');

const PORT = 8880;
const app = express();

const isDev = process.env.NODE_ENV === 'dev';

const routes = require('./server/routes/index');

app.use("/", routes);

if(isDev) {
    const webpackDevMiddleware = require("webpack-dev-middleware");
    const webpackHotMiddleware = require("webpack-hot-middleware");
    const webpack = require("webpack");
    const webpackConfig = require("./build/webpack.dev.conf.js");
    const compiler = webpack(webpackConfig, function () {
        console.log("---compiler---")
      });
    console.log(typeof compiler.plugin)
                                                         
    app.use(webpackDevMiddleware(compiler, {
        hot: true,
        // filename: './src/main.js',
        publicPath: webpackConfig.output.publicPath,
        stats: {
            colors: true
        },
        // historyApiFallback: true
    }));

    // app.use(webpackHotMiddleware(compiler, {
    //     log: console.log,
    //     // 这个path对应entry里的path
    //     path: '/__webpack_hmr',
    //     heartbeat: 2 * 1000
    // }));
    // app.use(webpackHotMiddleware(compiler));

    const server = http.createServer(app);

    app.use(express.static(path.join(__dirname, './static')));

    server.listen(8885, function(){
        console.log('App (dev) is now running on PORT '+ 8885 +'!')
    });
}else {
    app.use(express.static(path.join(__dirname, './static')));

    app.listen(PORT, function () {
        console.log('App (production) is now running on PORT ' + PORT + '!')
    });
}